import Foundation
import Logging
import Standard

public enum Loggers {}

private extension Loggers {
    static let lock = ReadWriteLock()
    static var loggers = [String: Logging.Logger]()
}

public extension Loggers {
    static subscript(name: String) -> Logging.Logger {
        lock.lockRead()
        if let logger = loggers[name] {
            defer { lock.unlock() }
            return logger
        } else {
            lock.unlock()
            let logger = make(name: name)
            lock.lockWrite()
            defer { lock.unlock() }
            loggers[name] = logger
            return logger
        }
    }
}

private extension Loggers {
    static func make(name: String) -> Logging.Logger {
        var logger = Logger(label: name)
        logger.logLevel = .trace
        return logger
    }
}
